package com.itheima.aop;


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;


//@Order(1)  // 控制执行顺序--> 这个优先级就是最高了
@Slf4j
//@Aspect  // 生命切片类
//@Component // 将本类交给ioc容器进行管理
public class MyAspect3 {

    //抽取公共切入点表达式
//    @Pointcut("execution(* com.itheima.service.impl.DeptServiceImpl.*(..))")
    //完整的方法签名--> 进行匹配
    //@Pointcut("execution(public void com.itheima.service.impl.DeptServiceImpl.delete(java.lang.Integer))")
    //可以省略访问修饰符
    @Pointcut("execution(void com.itheima.service.impl.DeptServiceImpl.delete(java.lang.Integer))")
    public void pt() {
    }


    //环绕前通知
    //@Around("execution(* com.itheima.service.impl.DeptServiceImpl.*(..))")
    @Around("pt()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("环绕前通知....");
        Object result = joinPoint.proceed();
        log.info("环绕后通知....");
        //注意是必须要进行返回的, 就是被增强的方法没有返回也要返回, null也是没有影响的
        return result;
    }

    //目标方法前触发该通知
//    @Before("execution(* com.itheima.service.impl.DeptServiceImpl.*(..))")
    @Before("pt()")
    public void before(JoinPoint joinPoint) {
        log.info("前置通知...");
        System.out.println("joinPoint.getArgs() = " + joinPoint.getArgs());
    }

    //无论目标方法, 是否有异常, 都会出发该通知
//    @After("execution(* com.itheima.service.impl.DeptServiceImpl.*(..))")
    @After("pt()")
    public void after(JoinPoint joinPoint) {
        log.info("{}", joinPoint);
        log.info("后置通知....");
    }

    //只有目标方法正常返回后, 才会触发该通知
//    @AfterReturning("execution(* com.itheima.service.impl.DeptServiceImpl.*(..))")
    @AfterReturning("pt()")
    public void afterReturning() {
        log.info("返回后通知....");
    }

    //只有目标方法抛异常了, 才会触发该通知
//    @AfterThrowing("execution(* com.itheima.service.impl.DeptServiceImpl.*(..))")
    @AfterThrowing("pt()")
    public void afterThrowing(JoinPoint joinPoint) {
        log.info("异常后通知....");
    }

}
